package com.tarena.lbs.activity.controller;

import com.tarena.lbs.activity.pojo.param.ActivityAddParam;
import com.tarena.lbs.activity.pojo.query.ActivityQuery;
import com.tarena.lbs.activity.pojo.vo.ActivityVO;
import com.tarena.lbs.activity.service.ActivityService;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Map;

/**
 * 活动相关功能接口
 */
@RestController
@Tag(name = "活动服务", description = "活动服务")
@Slf4j
public class ActivityController {

    @Autowired
    private ActivityService activityService;


    @Operation(summary = "商家活动列表")
    @PostMapping("/admin/marketing/marketingActivity/info/list")
    public Result<PageResult<ActivityVO>> pageList(@RequestBody ActivityQuery query)throws BusinessException {
        log.info("查询商家活动列表入参:{}",query);
        PageResult<ActivityVO> voPage = activityService.pageList(query);
        return new Result<>(voPage);
    }

    @Operation(summary = "商家活动保存")
    @PostMapping("/admin/marketing/marketingActivity/info/save")
    public Result<Void> activitySave(@RequestBody ActivityAddParam param)throws BusinessException, ParseException {
        log.info("商家活动保存入参:{}", param);
        activityService.updateActivity(param);
        return Result.success();
    }

    @Operation(summary = "商家活动编辑")
    @PostMapping("/admin/marketing/marketingActivity/info/update")
    public Result<Void> activityUpdate(@RequestBody ActivityAddParam param)throws BusinessException, ParseException {
        log.info("商家活动编辑入参:{}", param);
        activityService.updateActivity(param);
        return Result.success();
    }

    @Operation(summary = "商家活动详情")
    @GetMapping("/admin/marketing/marketingActivity/info/detail/{id}")
    public Result<ActivityVO> activityDetail(@PathVariable("id") Integer id)throws BusinessException {
        // 添加参数校验
        if (id == null || id <= 0) {
            log.warn("查询商家活动详情参数无效: id={}", id);
            return new Result<>(null);
        }
        
        log.info("查询商家活动详情入参:{}",id);
        try {
            ActivityVO activityVO=activityService.activityDetail(id);
            log.info("查询商家活动详情成功，出参:{}",activityVO);
            return new Result<>(activityVO);
        } catch (Exception e) {
            log.error("查询商家活动详情异常，id={}", id, e);
            return new Result<>(null);
        }
    }

    @Operation(summary = "商家活动状态修改")
    @PostMapping("/admin/marketing/marketingActivity/info/updateStatus")
    public Result<Void> updateStatus(@RequestBody Map<String,Integer> param)throws BusinessException {
        log.info("商家活动状态修改入参:{}", param);
        activityService.updateStatus(param);
        return Result.success();
    }

    @Operation(summary = "商家活动删除")
    @PostMapping("/admin/marketing/marketingActivity/info/delete/{id}")
    public Result<Void> delete(@PathVariable("id") Integer id)throws BusinessException {
        log.info("商家活动删除入参:{}", id);
        activityService.delete(id);
        return Result.success();
    }

}
